(function () {
    initDate();
    validateRule();
    var logId = $("#logisticId").val();
    getLogNameByUpdate(logId);

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#signupForm").validate({
            rules: {
                name: {
                    required: true
                }
            },
            messages: {
                name: {
                    required: icon + "请输入姓名"
                }
            },
            submitHandler: function () {
                var data = buildData();
                // console.log(data);
                // if (!data.products || data.products.length == 0) {
                //     layer.alert("请选择产品");
                //     return false
                // }
                $.ajax({
                    url: getProjectName()+"/invoice/edit",
                    type: "POST",
                    contentType: "application/json",
                    dataType: "JSON",
                    data: JSON.stringify(data),// 你的formid
                    success: function (data) {
                        if (data.code == 0) {
                            parent.layer.msg("操作成功");
                            var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                            parent.layer.close(index);
                            parent.reLoad();
                        } else {
                            parent.layer.alert(data.msg)
                        }

                    },
                    error: function (request) {
                        parent.layer.alert("Connection error");
                    }
                });
                return false;
            }
        })
    }


    function buildData() {
        var data = {};
        data.invoiceId = $("#signupForm input[name=invoiceId]").val();
        data.agentId = $("#signupForm input[name=agentId]").val();
        data.linkman = $("#signupForm input[name=linkman]").val();
        data.tel = $("#signupForm input[name=tel]").val();
        data.deliverAddress = $("#signupForm input[name=deliverAddress]").val();
        data.deliverDate = Date.parse($("#signupForm input[name=deliverDate]").val());
        data.remark = $("#signupForm input[name=remark]").val();
        data.code = $("#signupForm input[name=code]").val();
        data.logisticsId = $("#signupForm select[name=logisticsId] option:selected").val();

        // data.products = buildProducts();
        return data;
    }

    // function buildProducts() {
    //     var products = [];
    //     $("#signupForm .product-list div[class =product-group]").each(function () {
    //         var product = {};
    //         product.productId = $(this).children("input[name=number]").attr("id");
    //         product.productSize = $(this).children("input[name=number]").val();
    //         products.push(product);
    //     });
    //     return products;
    // }


    var productIndex, agentIndex;
    $(document).on("click", "a,i", function () {
        switch ($(this).attr("option")) {
            case "agentSelect": {
                agentSelect();
                break;
            }
        }
    });

    /**
     *
     */
    function agentSelect() {
        agentIndex = layer.open({
            type: 2,
            title: '选择经销商',
            maxmin: false,
            shadeClose: false, // 点击遮罩关闭层
            area: ['100%', '100%'],
            content: getProjectName()+'/invoice/view/agent/select' // iframe的url
        });
    }


    /**
     *
     * @param data
     */
    window.agentCallback = function (data) {
        $("#agentId").val(data.productId);
        $("#agentName").val(data.agentName);
        $("#tel").val(data.tel);
        $("#linkman").val(data.linkman);
        $("#deliverAddress").val(data.address);
        layer.close(agentIndex);
    };
})();

